package com.iking.housekeeping.controller.elderlyrelation;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.iking.housekeeping.common.CodeEnum;
import com.iking.housekeeping.common.PageParam;
import com.iking.housekeeping.common.PageResult;
import com.iking.housekeeping.common.ReturnCode;
import com.iking.housekeeping.entity.elderlyrelation.ElderlyRelation;
import com.iking.housekeeping.service.elderlyrelation.ElderlyRelationService;
import com.iking.housekeeping.util.UUIDUtil;

/**
 * 家属
 * 
 * @author: gf
 * @date: 2018年4月23日 上午11:59:37
 */
@RestController
@RequestMapping("/elderlyRelation")
public class ElderlyRelationController {
	
	/**
	 * 家属操作
	 */
	@Autowired
	private ElderlyRelationService elderlyRelationService;	
	
	/**
	 * 	添加家属
	 *  @param req
	 *  @param bindingResult
	 *  @return ReturnCode
	 *  @author gf
	 */
	@PostMapping("/insert")
	public ReturnCode insert(@RequestBody @Valid ElderlyRelation req, BindingResult bindingResult) {
		//验证参数
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			return new ReturnCode(CodeEnum.FAIL_EARROR_PARM, msg, null);
		}
		req.setfId(UUIDUtil.getUUID());
		req.setfIsdelete(false);
		//执行添加
		elderlyRelationService.insert(req);
		return new ReturnCode(CodeEnum.SUCCESS, req);
	}
	
	/**
	 * 	删除家属
	 *  @param fId
	 *  @return ReturnCode
	 *  @author gf
	 */
	@GetMapping("/delete/{fId}")
	public ReturnCode delete(@PathVariable("fId") String fId ) {
		//执行删除
		elderlyRelationService.delete(fId);
		return new ReturnCode(CodeEnum.SUCCESS);
	}
	
	/**
	 * 	更新
	 *  @param req
	 *  @param bindingResult
	 *  @return ReturnCode
	 *  @author gf
	 */
	@PostMapping("/update")
	public ReturnCode update(@RequestBody @Valid ElderlyRelation req, BindingResult bindingResult) {
		//验证参数
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			return new ReturnCode(CodeEnum.FAIL_EARROR_PARM, msg);
		}
		//执行更新
		elderlyRelationService.update(req);
		return new ReturnCode(CodeEnum.SUCCESS, req);
	}
	
	/**
	 * 	分页查询家属信息
	 *  @param pageParam
	 *  @param bindingResult
	 *  @return ReturnCode
	 *  @author gf
	 */
	@PostMapping("selectByPage")
	public ReturnCode seleteByPage(@RequestBody @Valid PageParam pageParam, BindingResult bindingResult) {
		//验证参数
		if (bindingResult.hasErrors()) {
			String msg = bindingResult.getFieldError().getDefaultMessage();
			return new ReturnCode(CodeEnum.FAIL_EARROR_PARM, msg, null);
		}
		//执行查询
		Page<ElderlyRelation> page = elderlyRelationService.selectByPage(pageParam);
		return new ReturnCode(CodeEnum.SUCCESS, new PageResult<ElderlyRelation>(page));
	}
}
